package cn.fbs.service;

import cn.fbs.dao.dm.DMCodeCheckMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author 安逸
 * @date 2021/1/26 - 15:18
 **/
@Service
public class CheckService {
    @Autowired
    private DMCodeCheckMapper tCodeCheckMapper;

    /**
     * 查询资料图片视频审查内容
     * @param id
     * @param connectionType
     * @return
     */
    public Map<String,Object> querydataInfo(String id,int connectionType) {

        //定义一个map存入参数
        Map paramMap = new HashMap();
        paramMap.put("id",id);
        paramMap.put("connectionType",connectionType);


        Map<String,Object> map =  tCodeCheckMapper.querydataInfo(paramMap);


        if (map!=null){


            //查询现已有的标签
            List<Map<String, Object>> maps = tCodeCheckMapper.queryDataCategory(paramMap);

            List categoryId = new ArrayList();

            for (Map map1 : maps){

                categoryId.add(map1.get("category_id").toString());
            }

            //查询除了现已打标签打打其他固有标签
            List<Map<String,Object>> list1 = tCodeCheckMapper.queryCategory(categoryId);

            map.put("isCategory",maps);
            map.put("noCategory",list1);
        }

        return map;

    }

// TODO: 2021/2/22 时间排序

    /**
     * 审查标签的修改
     * @param id
     * @param categoryId
     * @param userId
     *
     * 修改标签则前端传入修改后的标签id
     * 首先删除中间表中对应message_id所有对应关系
     * 然后依次录入新的对应关系
     * categoryId为“,”分割的字符串
     */
    private void updateCategory(String id,String categoryId,String userId,int connectionType){

        //categoryId为“,”分割的字符串
        String[] split = categoryId.split(",");

        //组织参数
        Map paraMap = new HashMap();
        paraMap.put("id",id);
        paraMap.put("userId",userId);

        //首先删除对应id的中间表中所有关系
        int i = tCodeCheckMapper.deleteConnectionById(paraMap);

        //依次循环录入相关关系

        if (i>0){
            for (int i1 = 0; i1 < split.length; i1++) {
                Map paramMap = new HashMap();
                paramMap.put("id",id);
                paramMap.put("categoryId",split[i1]);
                paramMap.put("userId",userId);
                paramMap.put("connectionType",connectionType);   //connectionType 1是资料 2是图片3是视频
                tCodeCheckMapper.saveConnection(paramMap);
            }
        }
    }


    /**
     * 资料 图片 视频 审核功能
     * @param fileName 文件名
     * @param source 来源
     * @param fileSize 大小
     * @param id
     * @param categoryId 标签id
     * @param userId  用户id
     * @param connectionType  关联类型 1是资料 2是图册
     * @return
     */
    public Map<String,Object> updatedataInfo(String fileName,String source,String fileSize,String id,String categoryId,String userId,int connectionType){
        //定义返回值
        Map resultMap = new HashMap();
        //组织参数
        Map paramMap = new HashMap();
        paramMap.put("fileName",fileName);
        paramMap.put("source",source);
        paramMap.put("fileSize",fileSize);
        paramMap.put("id",id);
        paramMap.put("connectionType",connectionType);



        //编辑资料 或 图册信息
        int i = tCodeCheckMapper.updatedataInfo(paramMap);
        if (i>0){
            //如果标签修改 更新操作
            updateCategory(id,categoryId,userId,connectionType);
            resultMap.put("message","更新成功");
            resultMap.put("status","success");
            resultMap.put("code",1);
        }else {
            resultMap.put("message","更新失败");
            resultMap.put("status","faile");
            resultMap.put("code",0);
        }
        return resultMap;
    }

    /**
     * 资料 图片 视频评分
     * @param id
     * @param score
     * @param evaluate
     * @param connectionType
     * @return
     */
    public Map<String,Object> savedatatEvaluate(String id,String score,String evaluate,int connectionType,String userId){
        //定义返回值
        Map resultMap = new HashMap();
        //组织参数
        Map paramMap = new HashMap();
        paramMap.put("id",id);
        paramMap.put("score",score);
        paramMap.put("evaluate",evaluate);
        paramMap.put("connectionType",connectionType);
        paramMap.put("userId",userId);
        System.out.println(paramMap);
        //存入数据库中
        int i = tCodeCheckMapper.savedataEvaluate(paramMap);
        if (i>0){
            resultMap.put("message","更新成功");
            resultMap.put("status","success");
            resultMap.put("code",1);
        }else {
            resultMap.put("message","更新失败");
            resultMap.put("status","faile");
            resultMap.put("code",0);
        }
        return resultMap;
    }

    /**
     * 文件上传
     */

    public String uploadFile(MultipartFile file){
        if (file.isEmpty()){
            return "上传失败 ";
        }
        //获取后缀名
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        //创建新的后缀名
        String fileName = UUID.randomUUID().toString()+extName;

        //文件路径
        String filepath = null;
        if(extName.contains("jpg")||extName.contains("png")||extName.contains("pdf")){
            filepath = "/data/pic";
        }else if (extName.contains("doc")||extName.contains("docx")){
            filepath="/data/file";
        }else if (extName.contains("mp4")||extName.contains("avi")||extName.contains("rmvb")){
            filepath="/data/video";
        }

        File dest = new File(filepath + fileName);

        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }


}
